package vista;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.util.LinkedList;

import javax.swing.JList;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;


import vista.listeners.AjustarComponenteListener;
import vista.listeners.BotonAceptarAjustesListener;
import vista.listeners.BotonVolverListener;
import vista.listeners.SlideMoverListener;

import modelo.auto.ElementoTaller;
import modelo.escenario.Taller;

import controlador.StreetRoadApplication;
import controlador.taller.ControladorTaller;
public class PanelAjustar extends javax.swing.JPanel {
	/**
	 * 
	 */
	// CONSTANTES PARA EL SLIDER
	private static final int INICIO = 0;
	private static final int FIN = 100;
	private int ACTUAL = 15;   
	
	private static final long serialVersionUID = 1L;
	private Taller taller;
    
	/** Creates new form PanelAjustar */
    public PanelAjustar(Taller taller) {
    	this.taller = taller;
        initComponents();
    }
    
    // carga un vector de Strings con los tipos
    private String[] cargarlista( Taller taller){
    	LinkedList<ElementoTaller> lista= taller.getListaComponentesAuto(StreetRoadApplication.getInstancia().getUsuarioActual());
    	int size= lista.size();
    	Iterator <ElementoTaller> ite = lista.iterator();
    	String[] tipos= new String[size];
		int i=0;
		while (ite.hasNext()){
			tipos[i]= ite.next().getTipo();
			i++;
		}
		return tipos;
    }
    
    private void disableAjuste(){
        jSlider1.enable(false);
        //botonAceptar.enable(false);
        botonCancelar.enable(false);
    }
    
    
    // para mostrar el label de nombre de usuario actual y dinero disponible
   public void mostrarUsuarioyDinero(){
    	String nombreUsr= StreetRoadApplication.getInstancia().getNombreUsuarioActual();
    	labelNombre.setText(nombreUsr);
    	
    	double dineroUsr = StreetRoadApplication.getInstancia().getDineroUsuarioActual();
    	labelDinero.setText(Double.toString(dineroUsr)+ " Algo$");
    	
    	
    }
    
   
   
   public void actualizarLista(){
       // importante aqui se carga la Jlist
       jList1.setModel(new javax.swing.AbstractListModel() {
          String[] strings = cargarlista(taller) ;
          
           public int getSize() { return strings.length; }
           public Object getElementAt(int i) { return strings[i]; }
       });
   }
   
   
   //terminar
   ListSelectionListener lsl= new ListSelectionListener(){
   	public void valueChanged(ListSelectionEvent e) {

   		int indiceseleccionado= ((JList) e.getSource()).getSelectedIndex();
       	LinkedList<ElementoTaller> lista= taller.getListaComponentesAuto(StreetRoadApplication.getInstancia().getUsuarioActual());
       	
       	//cambio label descripcion del componente
       	String descripcion = lista.get(indiceseleccionado).getDescripcion();
       	labelDescripcion.setText(descripcion);
       	
       /*	//cambio el label del precio del componente seleccionado
       	
       	double precio= lista.get(indiceseleccionado).getPrecio();
       	String mostrar= Double.toString(precio);
       	
       	labelPrecio.setText(precio+ " Algo$");
       	
       	*/
       	
       	//cambio el valor de vida util en el label y slider
       	//actualizarLista();
   	}
   };
   
   
   //modif1.lmt
   ActionListener listenerAjustar= new ActionListener(){
	   	public void actionPerformed(ActionEvent e) {
	   		// 1. ver cual item de la Jlist esta seleccionado
	   		int indiceSeleccionado =jList1.getSelectedIndex();
	   		
	   		//2. intentar ajustarlo
	   		ControladorTaller ct = new ControladorTaller(taller);
	   		ct.ajustarComponente(indiceSeleccionado);
	   	
	   		//3. actualizar dinero actual
	   		mostrarUsuarioyDinero();
	   	}
   };
   
   
   
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        
        
        jList1 = new javax.swing.JList();
        jLabel1 = new javax.swing.JLabel();
        labelDescripcion = new javax.swing.JLabel();
        botonAjustarComp = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jSlider1 = new javax.swing.JSlider(INICIO, FIN, ACTUAL);
        labelVidaUtil = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        labelPrecio = new javax.swing.JLabel();
        botonAceptar = new javax.swing.JButton();
        botonCancelar = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        labelNombre = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        labelDinero = new javax.swing.JLabel();

        setMaximumSize(new java.awt.Dimension(800, 600));
        setMinimumSize(new java.awt.Dimension(800, 600));

        // importante aqui se carga la Jlist
        actualizarLista();
        
        jList1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(jList1);

        jLabel1.setText("Descripcion:");

        labelDescripcion.setText("aqui va la descripcion");

        botonAjustarComp.setText("Ajustar Componente");

        jLabel3.setText("Vida Util:");

        labelVidaUtil.setText("valor");

        jLabel5.setText("Precio:");

        labelPrecio.setText("xx Algo$");

        botonAceptar.setText("Aceptar"); 

        botonCancelar.setText("Cancelar");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(27, 27, 27)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel5))
                        .addGap(12, 12, 12)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(labelVidaUtil)
                                .addGap(18, 18, 18)
                                .addComponent(jSlider1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(labelPrecio, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(53, 53, 53)
                        .addComponent(botonAceptar)
                        .addGap(73, 73, 73)
                        .addComponent(botonCancelar)))
                .addContainerGap(43, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jSlider1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel3)
                        .addComponent(labelVidaUtil)))
                .addGap(47, 47, 47)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(labelPrecio))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botonAceptar)
                    .addComponent(botonCancelar))
                .addContainerGap(26, Short.MAX_VALUE))
        );

        jButton4.setText("Volver");

        jLabel7.setText("Nombre:");

        labelNombre.setText("nombre");

        jLabel9.setText("Dinero");

        labelDinero.setText("xx Algo$");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(73, 73, 73)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 211, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(46, 46, 46)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(labelDescripcion, javax.swing.GroupLayout.PREFERRED_SIZE, 307, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(155, 155, 155)
                        .addComponent(botonAjustarComp))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(31, 31, 31)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jButton4)
                            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(97, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(489, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel7)
                    .addComponent(jLabel9))
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelDinero)
                    .addComponent(labelNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(130, 130, 130))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(37, 37, 37)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(labelNombre))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(labelDinero))
                .addGap(51, 51, 51)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(40, 40, 40)
                        .addComponent(labelDescripcion, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(botonAjustarComp)
                        .addGap(18, 18, 18)
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 398, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton4)
                .addContainerGap(33, Short.MAX_VALUE))
        );
 
        disableAjuste();
        
        //Agrego Listeners
        
        jList1.setSelectedIndex(1);
        jList1.addListSelectionListener(lsl);
        jButton4.addActionListener(new BotonVolverListener());
        jPanel1.setBackground(Color.GRAY);
        

        
        //modif1.lmt
        
        botonCancelar.addActionListener(new BotonVolverListener());
        
        
        botonAjustarComp.addActionListener(listenerAjustar); //new AjustarComponenteListener(jList1.getSelectedIndex(), StreetRoadApplication.getInstancia().getUsuarioActual()));
        botonAceptar.addActionListener(new BotonAceptarAjustesListener());
        jSlider1.addChangeListener(new SlideMoverListener());
        
    }// </editor-fold>


    // Variables declaration - do not modify
    private javax.swing.JButton botonAjustarComp;
    private javax.swing.JButton botonAceptar;
    private javax.swing.JButton botonCancelar;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel labelDinero;
    private javax.swing.JLabel labelDescripcion;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel labelVidaUtil;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel labelPrecio;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel labelNombre;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JList jList1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSlider jSlider1;
    // End of variables declaration

}
